import { EVENTS, type EventConfig } from '../../events';
import { STORY_ID } from '../story';

export const REALM_BIG_LEVEL_TYPE = {
  FAN_REN: 'FAN_REN',
  LIAN_QI: 'LIAN_QI',
  ZHU_JI: 'ZHU_JI',
  JIE_DAN: 'JIE_DAN',
  YUAN_YING: 'YUAN_YING',
  HUA_SHEN: 'HUA_SHEN',
  LIAN_XU: 'LIAN_XU',
  HE_TI: 'HE_TI',
  DA_CHENG: 'DA_CHENG',
  ZHEN_XIAN: 'ZHEN_XIAN',
  JIN_XIAN: 'JIN_XIAN',
  TAI_YI: 'TAI_YI',
  DA_LUO: 'DA_LUO',
  DAO_ZU: 'DAO_ZU',
} as const;

export const REALM_BIG_LEVEL_TYPE_NAME_MAP: Record<RealmBigLevelType, string> =
  {
    FAN_REN: '凡人',
    LIAN_QI: '练气',
    ZHU_JI: '筑基',
    JIE_DAN: '结丹',
    YUAN_YING: '元婴',
    HUA_SHEN: '化神',
    LIAN_XU: '炼虚',
    HE_TI: '合体',
    DA_CHENG: '大乘',
    ZHEN_XIAN: '真仙',
    JIN_XIAN: '金仙',
    TAI_YI: '太乙',
    DA_LUO: '大罗',
    DAO_ZU: '道祖',
  };

export const REALM_BIG_LEVEL_NUMBER_MAP: Record<RealmBigLevelType, number> = {
  FAN_REN: 1,
  LIAN_QI: 2,
  ZHU_JI: 3,
  JIE_DAN: 4,
  YUAN_YING: 5,
  HUA_SHEN: 6,
  LIAN_XU: 7,
  HE_TI: 8,
  DA_CHENG: 9,
  ZHEN_XIAN: 10,
  JIN_XIAN: 11,
  TAI_YI: 12,
  DA_LUO: 13,
  DAO_ZU: 14,
};

export type RealmBigLevelType =
  (typeof REALM_BIG_LEVEL_TYPE)[keyof typeof REALM_BIG_LEVEL_TYPE];

export interface RealmLevel {
  name: string;
  /** 大境界类型 */
  bigLevelType: RealmBigLevelType;
  /** 小境界等级 */
  smallLevel: number;
  /** 下一境界需要经验 */
  requiredExp: number;
  /** 达到境界触发的事件 */
  event?: EventConfig[];
}

interface RealmExpConfig {
  /** 基础经验值 */
  baseExp: number;
  /** 每层经验增长系数(比如 1.5 表示每层增加 50%) */
  growthRate: number;
  /** 大境界跃迁时的经验跳跃倍数(比如 2 表示[金丹·初期]层是[筑基·大圆满]的 2 倍) */
  bigLevelJump: number;
  /** 全局难度系数(0.5 简单, 1.0 正常, 2.0 困难) */
  difficultyMultiplier: number;
}

const DEFAULT_CONFIG: RealmExpConfig = {
  baseExp: 100,
  growthRate: 1.4,
  bigLevelJump: 2.0,
  difficultyMultiplier: 1.0,
};

const DEFAULT_REALM_LAYER = 4;
export const REALM_LAYER_CONFIG: Record<string, number> = {
  [REALM_BIG_LEVEL_TYPE.FAN_REN]: 1,
  [REALM_BIG_LEVEL_TYPE.LIAN_QI]: 10,
  [REALM_BIG_LEVEL_TYPE.DAO_ZU]: 1,
};

/** 计算某个境界的下一境界需要经验 */
function calculateNextLevelRequiredExp(
  baseExp: number,
  smallLevel: number,
  config: RealmExpConfig = DEFAULT_CONFIG
): number {
  const { growthRate, difficultyMultiplier } = config;

  let exp = baseExp * Math.pow(growthRate, smallLevel - 1);

  exp *= difficultyMultiplier;

  return Math.ceil(exp);
}

export function generateRealmLevelList(config?: RealmExpConfig): RealmLevel[] {
  const list: RealmLevel[] = [];

  let baseExp = DEFAULT_CONFIG.baseExp;
  if (config) {
    baseExp = config.baseExp;
  }

  data.forEach((item, index) => {
    let requiredExp: number = 0;

    if (index === 0) {
      requiredExp = 0;
    } else {
      requiredExp = calculateNextLevelRequiredExp(
        baseExp,
        item.smallLevel,
        config
      );
      const layers =
        REALM_LAYER_CONFIG[item.bigLevelType] || DEFAULT_REALM_LAYER;
      if (item.smallLevel === layers) {
        baseExp = requiredExp * DEFAULT_CONFIG.bigLevelJump;
      }
    }

    list.push({
      ...item,
      requiredExp,
    });
  });

  return list;
}

const data: Omit<RealmLevel, 'requiredExp'>[] = [
  {
    name: '凡人',
    bigLevelType: REALM_BIG_LEVEL_TYPE.FAN_REN,
    smallLevel: 1,
  },
  {
    name: '练气·一层',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_QI,
    smallLevel: 1,
  },
  {
    name: '练气·二层',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_QI,
    smallLevel: 2,
  },
  {
    name: '练气·三层',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_QI,
    smallLevel: 3,
  },
  {
    name: '练气·四层',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_QI,
    smallLevel: 4,
  },
  {
    name: '练气·五层',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_QI,
    smallLevel: 5,
  },
  {
    name: '练气·六层',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_QI,
    smallLevel: 6,
  },
  {
    name: '练气·七层',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_QI,
    smallLevel: 7,
  },
  {
    name: '练气·八层',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_QI,
    smallLevel: 8,
  },
  {
    name: '练气·九层',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_QI,
    smallLevel: 9,
  },
  {
    name: '练气·大圆满',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_QI,
    smallLevel: 10,
  },
  {
    name: '筑基·初期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.ZHU_JI,
    smallLevel: 1,
  },
  {
    name: '筑基·中期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.ZHU_JI,
    smallLevel: 2,
  },
  {
    name: '筑基·后期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.ZHU_JI,
    smallLevel: 3,
  },
  {
    name: '筑基·大圆满',
    bigLevelType: REALM_BIG_LEVEL_TYPE.ZHU_JI,
    smallLevel: 4,
  },
  {
    name: '结丹·初期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.JIE_DAN,
    smallLevel: 1,
  },
  {
    name: '结丹·中期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.JIE_DAN,
    smallLevel: 2,
  },
  {
    name: '结丹·后期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.JIE_DAN,
    smallLevel: 3,
  },
  {
    name: '结丹·大圆满',
    bigLevelType: REALM_BIG_LEVEL_TYPE.JIE_DAN,
    smallLevel: 4,
  },
  {
    name: '元婴·初期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.YUAN_YING,
    smallLevel: 1,
  },
  {
    name: '元婴·中期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.YUAN_YING,
    smallLevel: 2,
  },
  {
    name: '元婴·后期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.YUAN_YING,
    smallLevel: 3,
  },
  {
    name: '元婴·大圆满',
    bigLevelType: REALM_BIG_LEVEL_TYPE.YUAN_YING,
    smallLevel: 4,
  },
  {
    name: '化神·初期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.HUA_SHEN,
    smallLevel: 1,
  },
  {
    name: '化神·中期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.HUA_SHEN,
    smallLevel: 2,
  },
  {
    name: '化神·后期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.HUA_SHEN,
    smallLevel: 3,
  },
  {
    name: '化神·大圆满',
    bigLevelType: REALM_BIG_LEVEL_TYPE.HUA_SHEN,
    smallLevel: 4,
  },
  {
    name: '炼虚·初期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_XU,
    smallLevel: 1,
  },
  {
    name: '炼虚·中期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_XU,
    smallLevel: 2,
  },
  {
    name: '炼虚·后期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_XU,
    smallLevel: 3,
  },
  {
    name: '炼虚·大圆满',
    bigLevelType: REALM_BIG_LEVEL_TYPE.LIAN_XU,
    smallLevel: 4,
  },
  {
    name: '合体·初期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.HE_TI,
    smallLevel: 1,
  },
  {
    name: '合体·中期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.HE_TI,
    smallLevel: 2,
  },
  {
    name: '合体·后期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.HE_TI,
    smallLevel: 3,
  },
  {
    name: '合体·大圆满',
    bigLevelType: REALM_BIG_LEVEL_TYPE.HE_TI,
    smallLevel: 4,
  },
  {
    name: '大乘·初期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.DA_CHENG,
    smallLevel: 1,
  },
  {
    name: '大乘·中期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.DA_CHENG,
    smallLevel: 2,
  },
  {
    name: '大乘·后期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.DA_CHENG,
    smallLevel: 3,
  },
  {
    name: '大乘·大圆满',
    bigLevelType: REALM_BIG_LEVEL_TYPE.DA_CHENG,
    smallLevel: 4,
  },
  {
    name: '真仙·初期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.ZHEN_XIAN,
    smallLevel: 1,
    event: [
      {
        cmd: EVENTS.STORY_START,
        storyId: STORY_ID.ZHEN_XIAN,
      },
    ],
  },
  {
    name: '真仙·中期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.ZHEN_XIAN,
    smallLevel: 2,
  },
  {
    name: '真仙·后期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.ZHEN_XIAN,
    smallLevel: 3,
  },
  {
    name: '真仙·大圆满',
    bigLevelType: REALM_BIG_LEVEL_TYPE.ZHEN_XIAN,
    smallLevel: 4,
  },
  {
    name: '金仙·初期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.JIN_XIAN,
    smallLevel: 1,
  },
  {
    name: '金仙·中期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.JIN_XIAN,
    smallLevel: 2,
  },
  {
    name: '金仙·后期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.JIN_XIAN,
    smallLevel: 3,
  },
  {
    name: '金仙·大圆满',
    bigLevelType: REALM_BIG_LEVEL_TYPE.JIN_XIAN,
    smallLevel: 4,
  },
  {
    name: '太乙·初期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.TAI_YI,
    smallLevel: 1,
  },
  {
    name: '太乙·中期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.TAI_YI,
    smallLevel: 2,
  },
  {
    name: '太乙·后期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.TAI_YI,
    smallLevel: 3,
  },
  {
    name: '太乙·大圆满',
    bigLevelType: REALM_BIG_LEVEL_TYPE.TAI_YI,
    smallLevel: 4,
  },
  {
    name: '大罗·初期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.DA_LUO,
    smallLevel: 1,
  },
  {
    name: '大罗·中期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.DA_LUO,
    smallLevel: 2,
  },
  {
    name: '大罗·后期',
    bigLevelType: REALM_BIG_LEVEL_TYPE.DA_LUO,
    smallLevel: 3,
  },
  {
    name: '大罗·大圆满',
    bigLevelType: REALM_BIG_LEVEL_TYPE.DA_LUO,
    smallLevel: 4,
  },
  {
    name: '道祖',
    bigLevelType: REALM_BIG_LEVEL_TYPE.DAO_ZU,
    smallLevel: 1,
  },
];

export const REALM_LEVEL_LIST: RealmLevel[] = generateRealmLevelList();
